<!-- 
  @Description: 工单详情对话框

 -->
<template>
  <el-dialog v-model="dialogVisible" title="工单详情" width="80%" style="max-width: 1200px" draggable @closed="handlerClosed">
    <el-form ref="formRef" style="width: 100%" :model="formData" :rules="formRules" label-width="90px">
      <el-form-item label="标题">
        <el-input v-model="formData.name" disabled style="width: 100%" placeholder="请输入标题" />
      </el-form-item>
      <el-row>
        <el-col :span="8">
          <el-form-item label="类型">
            <el-select v-model="formData.woCategory" disabled placeholder="请选择类型" style="width: 100%">
              <el-option v-for="item in woCategoryOptions" :key="item.value" :label="item.label" :value="item.value" />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="状态">
            <el-radio-group v-model="formData.woStatus" disabled>
              <el-radio v-for="item in woStatusOptions" :key="item.value" :value="item.value">{{ item.label }}</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="优先级">
            <el-radio-group v-model="formData.woPriority" disabled>
              <el-radio v-for="item in woPriorityOptions" :key="item.value" :value="item.value">{{ item.label }}</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
      </el-row>
      <el-form-item label="描述">
        <Editor ref="editorRef" v-if="formData.description" :read-only="true" :show-toolbar="false" v-model="formData.description" editorHeight="500px" />
      </el-form-item>
      <el-form-item>
        <div style="display: flex; justify-content: flex-end; width: 100%">
          <el-button type="primary" @click="handlerClosed()">关闭</el-button>
        </div>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>
<script lang="ts" setup>
import { ref } from 'vue'
import type { FormInstance } from 'element-plus'
// import * as WorkOrder from '../../api/WorkOrder'
import { woCategoryOptions, woStatusOptions, woPriorityOptions } from './WorkOrderOptions'
import { useUsersStore } from '../../stores'
import Editor from '../../components/Editor.vue'
// import { ElMessage } from 'element-plus'
import _ from 'lodash'

const emits = defineEmits(['submited'])

const usersStore = useUsersStore()
// 表单引用
const formRef = ref<FormInstance>()

// 对话框是否显示
const dialogVisible = ref(false)

// 表单数据
const formData = ref({
  id: null as number | null,
  name: null as string | null,
  woCategory: null as string | null,
  userId: null as number | null,
  userName: null as string | null,
  woStatus: null as string | null,
  woPriority: null as string | null,
  woSolution: null as string | null,
  description: undefined as string | undefined, // 修改这一行
  imageUrls: [] as string[], // 用于保存上传图片的URL列表
})
// 表单验证规则
const formRules = ref({
  name: [{ required: true, message: '请输入标题', trigger: 'blur' }],
  woCategory: [{ required: true, message: '请选择类型', trigger: 'change' }],
  woStatus: [{ required: true, message: '请选择状态', trigger: 'change' }],
  woPriority: [{ required: true, message: '请选择优先级', trigger: 'change' }],
})

/**
 * 关闭对话框处理函数
 *
 * 该函数用于关闭当前显示的对话框，通过将dialogVisible响应式变量设置为false来隐藏对话框
 */
const handlerClosed = () => {
  dialogVisible.value = false
}

/**
 * 打开对话框
 *
 * @description 设置对话框可见状态为true，用于显示对话框
 * @returns {void}
 */
const open = (val: any) => {
  dialogVisible.value = true
  const { userId, username } = usersStore.user
  formData.value.userId = userId ?? null
  formData.value.userName = username ?? null
  if (val) {
    formData.value = val
    formData.value.description = _.unescape(val.description)
  }
}

defineExpose({ open })
</script>
<style lang="scss" scoped></style>
